/**
* 版权声明：厦门睿商网络科技有限公司 版权所有 违者必究
* 日    期：2020-05-12
*/
package com.rzico.order.entity;

import com.rzico.annotation.IgnoreSwaggerParameter;
import com.rzico.basics.entity.Supplier;
import lombok.Data;

import java.math.BigDecimal;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import javax.persistence.*;
import io.swagger.annotations.ApiModelProperty;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;

/**
 * <pre>
 *   退货单项数据库对象
 * </pre>
 * @author Rzico Boot
 * @version 1.0
 */
@Data
@JsonIgnoreProperties(value = {"handler"})
@JsonInclude(JsonInclude.Include.NON_NULL)
@Table(name = "wx_return")
public class Return{
    @Id
    @ApiModelProperty(value = "" )
    protected Long id;

    @ApiModelProperty(value = "创建日期" )
    protected java.util.Date createDate;

    @ApiModelProperty(value = "修改日期" )
    protected java.util.Date modifyDate;

    @JsonIgnore
    @ApiModelProperty(value = "是否删除" ,hidden = true )
    protected Boolean deleted;

    @ApiModelProperty(value = "送货单号" )
    protected String sn;

    @ApiModelProperty(value = "收货地址" )
    protected String address;

    @ApiModelProperty(value = "所在地" )
    protected String areaName;

    @ApiModelProperty(value = "收货人" )
    protected String consignee;

    @ApiModelProperty(value = "联系电话" )
    protected String phone;

    @ApiModelProperty(value = "备注" )
    protected String memo;

    @ApiModelProperty(value = "物流公司" )
    protected String deliveryCorp;

    @ApiModelProperty(value = "运单号" )
    protected String trackingNo;

    @ApiModelProperty(value = "配送方式(0.普通快递,1.到店核销,2.同城快送)" )
    protected Integer shippingMethod;

    @ApiModelProperty(value = "退货状态(1:未确认,3.已退货,4.已完成,5.已关闭)" )
    protected Integer shippingStatus;

    @ApiModelProperty(value = "买方会员")
    protected Long memberId;

    @ApiModelProperty(value = "来源订单")
    protected Long orderId;

    @ApiModelProperty(value = "配送计划" )
    protected Long orderPlanId;

    @ApiModelProperty(value = "来源订单号")
    protected String orderSn;

    @ApiModelProperty(value = "付款凭证号(第三方ID)")
    protected String tranSn;

    @ApiModelProperty(value = "申请单" )
    protected Long askforId;

    @ApiModelProperty(value = "送货线程")
    protected Long mapLineId;

    @ApiModelProperty(value = "出库单" )
    protected Long warehouseId;

    @ApiModelProperty(value = "所属企业")
    @Column(name = "enterprise_id")
    protected Long enterpriseId;

    @ApiModelProperty(value = "供应商(空为本部)" )
    protected Long supplierId;

    @ApiModelProperty(value = "发货仓库")
    protected Long shopId;

    @ApiModelProperty(value = "开票类型:0.不开票,1.增票,2.普票")
    protected Integer taxType;

    @ApiModelProperty(value = "楼层" )
    protected Integer level;

    @ApiModelProperty(value = "物流费用(所有)" )
    protected java.math.BigDecimal shippingFreight;

    @ApiModelProperty(value = "送货工资(含楼层费)" )
    protected java.math.BigDecimal adminFreight;

    @ApiModelProperty(value = "楼层费" )
    protected java.math.BigDecimal levelFreight;

    @ApiModelProperty(value = "实际销售日期")
    protected java.util.Date orderDate;

    @ApiModelProperty(value = "预约送达时间" )
    protected java.util.Date hopeDate;

    @ApiModelProperty(value = "预计送达时间" )
    protected java.util.Date estimateDate;

    @ApiModelProperty(value = "实际送达时间" )
    protected java.util.Date deliveryDate;

    @ApiModelProperty(value = "核销完成时间" )
    protected java.util.Date completeDate;

    @ApiModelProperty(value = "是否异常订单" )
    protected Boolean abnormal;

    @ApiModelProperty(value = "商品结算成本" )
    protected java.math.BigDecimal subCost;

    @ApiModelProperty(value = "商品合计金额" )
    protected java.math.BigDecimal subPrice;

    @ApiModelProperty(value = "商品合计数量" )
    protected java.math.BigDecimal subQuantity;

    @ApiModelProperty(value = "打印次数" )
    protected Integer sPrint;

    @ApiModelProperty(value = "所属部门" )
    protected String officeId;

    @ApiModelProperty(value = "送货员ID")
    protected String shippingEmployeeId;

    @JsonIgnore
    @ApiModelProperty(value = "创建者" ,hidden = true )
    protected String createBy;

    @JsonIgnore
    @ApiModelProperty(value = "更新者" ,hidden = true )
    protected String modifyBy;

    /**
     *  供应商
     */
    @JsonIgnore
    @IgnoreSwaggerParameter
    protected Supplier supplier;

    @ApiModelProperty(value = "配送单明细")
    List<ReturnItem> itemList = Collections.emptyList();


    @ApiModelProperty(hidden = true)
    @JsonIgnore
    public BigDecimal calculateSubQuantity(){
        BigDecimal totalQuantity = new BigDecimal(0);
        if (null != itemList && itemList.size() > 0){
            for (ReturnItem returnItem : itemList){
                totalQuantity = totalQuantity.add(returnItem.getQuantity());
            }
        }
        return totalQuantity;
    }

    @ApiModelProperty(hidden = true)
    @JsonIgnore
    public BigDecimal calculateSubPrice(){
        BigDecimal totalSubPrice = new BigDecimal(0);
        if (null != itemList && itemList.size() > 0){
            for (ReturnItem returnItem : itemList){
                BigDecimal subPriceTotal = returnItem.getPrice().multiply(returnItem.getQuantity());
                totalSubPrice =  totalSubPrice.add(subPriceTotal);
            }
        }
        return totalSubPrice;
    }


    @ApiModelProperty(hidden = true)
    @JsonIgnore
    public BigDecimal calculateSubCost(){
        BigDecimal totalSubPrice = new BigDecimal(0);
        if (null != itemList && itemList.size() > 0){
            for (ReturnItem returnItem : itemList){
                totalSubPrice =  totalSubPrice.add(returnItem.getCost().multiply(returnItem.getQuantity()));
            }
        }
        return totalSubPrice;
    }


    public String getSupplierName() {
        Supplier supplier = getSupplier();
        if (supplier!=null) {
            return supplier.getName();
        } else {
            return null;
        }
    }


    @ApiModelProperty(value = "退货状态")
    public String getStatusDescr() {
        if (getShippingStatus().equals(1)) {
            return "待确认";
        }
        if (getShippingStatus().equals(3)) {
            return "已退货";
        }
        if (getShippingStatus().equals(4)) {
            return "已完成";
        }
        if (getShippingStatus().equals(5)) {
            return "已关闭";
        }
        return "";
    }


}
